define(["jquery", "i18nCommon", "validate", "box", "cookie"], function ($, i18n, valid, box) {
    var Pager;

    Pager = function (settings) {
        if (!settings.after && !settings.before && !settings.parent && !settings.replace) {
            alert(i18n.textCommonParamError);
        }
        this.settings = $.extend({}, Pager.defaultSettings, settings);
    };

    Pager.defaultSettings = {
        after: null,
        before: null,
        parent: null,
        replace: null,
        pageInfo: null,
        changeHandler: function (data) {
        },
        cookie: false
    };

    Pager.prototype.init = function () {
        var that = this;
        this._pageElement = $(Pager.view);
        if (this.settings.after) {
            this.settings.after.before(this._pageElement);
        } else if (this.settings.before) {
            this.settings.before.after(this._pageElement);
        } else if (this.settings.parent) {
            this.settings.parent.append(this._pageElement);
        } else if (this.settings.replace) {
            this._pageElement.replaceAll(this.settings.replace);
        }
        this._pageElement.find(".app-pager-first").html("<i class='icon-double-angle-left'></i>");
        this._pageElement.find(".app-pager-last").html("<i class='icon-double-angle-right'></i>");
        //this._pageElement.find(".app-pager-previous").text("<");
        this._pageElement.find(".app-pager-previous").html("<i class='icon-angle-left'></i>");
        //this._pageElement.find(".app-pager-next").text(">");
        this._pageElement.find(".app-pager-next").html("<i class='icon-angle-right'></i>");
        this._pageElement.find(".app-pager-go").text("查询");
        this._pageElement.find(".app-pager-link").click($.proxy(that._linkHandler, that));
    };

    Pager.prototype._linkHandler = function (e) {
        var node = $(e.currentTarget);
        if (node.hasClass("app-pager-disabled") || node.hasClass("app-pager-nextpre-disabled")) return;
        if (node.hasClass("app-pager-first")) {
            this._firstPage();
        } else if (node.hasClass("app-pager-next")) {
            this._nextPage();
        } else if (node.hasClass("app-pager-previous")) {
            this._prevPage();
        } else if (node.hasClass("app-pager-last")) {
            this._lastPage();
        } else if (node.hasClass("app-pager-go")) {
            var page = this._pageElement.find("input").val();
            var validInt = valid.isInteger(page);
            if (!validInt.success || page === 0) {
                box.alert('格式不正确！请输入整数');
                return;
            }
            if (parseInt(page, 10)) {
                this.gotoPage(parseInt(page, 10));
            } else {
                this._pageElement.find("input").val(this.pageInfo.currentPage);
            }
        }
        this.settings.changeHandler(this._pageInfo);
    };

    Pager.prototype._firstPage = function () {
        this.gotoPage(1);
    };

    Pager.prototype._lastPage = function () {
        this.gotoPage(this._pageInfo.totalPage);
    };

    Pager.prototype._nextPage = function () {
        this.gotoPage(this._pageInfo.currentPage + 1);
    };

    Pager.prototype._prevPage = function () {
        this.gotoPage(this._pageInfo.currentPage - 1);
    };

    Pager.prototype.gotoPage = function (page) {
        if (page < 1) page = 1;
        if (page > this._pageInfo.totalPage) page = this._pageInfo.totalPage;
        this._pageInfo.currentPage = page;
    };

    Pager.prototype.setPageInfo = function (pageInfo) {
        var that = this;
        this._pageInfo = pageInfo;
        if (pageInfo.totalPage === 0) {
            this._pageElement.show();
        } else {
            this._pageElement.show();
            this._pageElement.find("input").removeAttr("disabled");
            this._pageElement.children().removeClass("app-pager-disabled");
            this._pageElement.find(".app-pager-previous").removeClass('app-pager-nextpre-disabled')
                .addClass("app-pager-nextpre-background");
            this._pageElement.find(".app-pager-next").removeClass('app-pager-nextpre-disabled')
                .addClass("app-pager-nextpre-background");
            if (pageInfo.currentPage === 1) {
                this._pageElement.find(".app-pager-first").addClass("app-pager-disabled");
                this._pageElement.find(".app-pager-previous").removeClass('app-pager-nextpre-background')
                    .addClass("app-pager-nextpre-disabled");
            }
            if (pageInfo.currentPage === pageInfo.totalPage) {
                this._pageElement.find(".app-pager-last").addClass("app-pager-disabled");
                this._pageElement.find(".app-pager-next").removeClass('app-pager-nextpre-background')
                    .addClass("app-pager-nextpre-disabled");
            }
        }
        this._pageElement.find("input").val(pageInfo.currentPage);
        if (!this.settings.cookie) {
            this._pageElement.find(".app-pager-msg").text(Pager.message.replace("x", pageInfo.currentPage).replace("y", pageInfo.totalPage).replace("c", pageInfo.currentRecord).replace("d", pageInfo.totalRecord));
        } else {
            var html = [];
            html.push('<span>' + pageInfo.currentPage + '/' +
                pageInfo.totalPage + ' 本页</span>');
            html.push('<input class="app-pager-msg-input" type="text" value="' + pageInfo.currentRecord + '" />');
            html.push('条,总数<span class="app-pager-msg-color">' + pageInfo.totalRecord + '</span>条');
            this._pageElement.find(".app-pager-msg").html('').append(html.join(''));
            this._pageElement.find(".app-pager-msg input").keydown(function (event) {
                if (event.keyCode === 13) {
                    var validInt = valid.isInteger($(this).val());
                    if (!validInt.success || $(this).val() === 0) {
                        box.alert('格式不正确！请输入整数');
                        return;
                    }
                    $.cookie(that.settings.cookieName, $(this).val(), {expires: 365});
                    that._pageInfo.currentPage = 1;
                    that._pageInfo.currentRecord = $(this).val();
                    that.settings.changeHandler(that._pageInfo);
                }
            });
        }
    };

    Pager.view = [
        "<div class='app-pager'>",
            "<div class='app-pager-first app-pager-link'></div>",
            "<div class='app-pager-previous app-pager-nextpre-background app-pager-link'></div>",
            "<div class='app-pager-next app-pager-nextpre-background app-pager-link'></div>",
            "<div class='app-pager-last app-pager-link'></div>",
            "<div class='app-pager-msg'></div>",
            "<div class='app-pager-jump'><input type='text'></div>",
            "<div class='app-pager-go app-pager-link app-pager-go-spe'></div>",
        "</div>"
    ].join("");

    Pager.message = "x/y(页) [c/d(条)]";

    /**
     * 用法：
     * var p = pager.getInstance({
			parent: $("#misTestPager")
			changeHandler: function(pageInfo){
			}
	   });
     p.setPageInfo(pageInfo);
     */
    return {
        getInstance: function (settings) {
            var p = new Pager(settings);
            p.init();
            return p;
        }
    }
});